﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace CAN_Controller
{
    public partial class MachinePanel : UserControl
    {
        private bool isOpen = false;
        private int mID;

        public bool IsOpen
        {
            get
            {
                return isOpen;
            }

            set
            {
                isOpen = value;
            }
        }

        public MachinePanel()
        {
            InitializeComponent();
        }
        public MachinePanel(int ID)
        {
            InitializeComponent();
            mID = ID;
            //            1  + 占位函数，不足7位补0  + i(占7位) = 8位   
            ID_Lb.Text = "1" + String.Format("{0:D7}", ID);
        }

        private void button_Click(object sender, EventArgs e)
        {
            switch (((Button)sender).Tag.ToString())
            {
                case "open":
                    {
                        open();
                        isOpen = true;
                    }
                    break;
                case "close":
                    {
                        close();
                        isOpen = false;
                    }
                    break;
                default: break;
            }
        }

        public void open()
        {
            string strID = Convert.ToString(mID, 16);
            //如果ID为 1 应该变为 01 传入
            if (strID.Length == 1)
                strID = "0" + Convert.ToString(mID, 16);
            //判断打开的模式
            string mode = "open";
            if(comboBox.SelectedIndex == 1)
            {
                mode = "open1_2";
            }
            else if (comboBox.SelectedIndex == 2)
            {
                mode = "open1_4";
            }
            //调用MainForm的sendMsg方法来发送信息
            MainForm.mFlagID = uint.Parse(strID);
            if (MainForm.sendMsg(strID, mode, "打开" + ID_Lb.Text + "错误") == false)
                return;
            openUI();
        }

        public void close()
        {
            string strID = Convert.ToString(mID, 16);
            //如果ID为 1 应该变为 01 传入
            if (strID.Length == 1)
                strID = "0" + Convert.ToString(mID, 16);
            //调用MainForm的sendMsg方法来发送信息
            MainForm.mFlagID = uint.Parse(strID);
            if (MainForm.sendMsg(strID, "close", "关闭" + ID_Lb.Text + "错误") == false)
                return;
            closeUI();
        }

        public void openUI()
        {
            //点亮红灯，绿灯变灰
            red_Img.BackColor = Color.Red;
            green_Img.BackColor = Color.Gray;

            //信息栏更新
            message_Lb.Text = comboBox.SelectedItem.ToString();
            time_Lb.Text = "时间：" + DateTime.Now.ToLongDateString() + DateTime.Now.ToLongTimeString();
        }

        public void closeUI()
        {
            //点亮绿灯，红灯变灰
            red_Img.BackColor = Color.Gray;
            green_Img.BackColor = Color.LimeGreen;

            //信息栏更新
            message_Lb.Text = "关";
            time_Lb.Text = "时间：" + DateTime.Now.ToLongDateString() + DateTime.Now.ToLongTimeString();
        }

        public void changeSelect(int index)
        {
            //设置下拉框中的选项
            comboBox.SelectedIndex = index;
        }
    }
}
